<template>
  <div>
    <el-card style="margin-top: 10px">
      <CategorySelector @changeCategory="changeCategory" :isShowCategory="isShowCategory"></CategorySelector>
    </el-card>

    <el-card style="margin-top: 10px">
      <!-- 列表项 -->
      <div v-show="!isShowSpuForm && !isShowSkuForm">
        <el-button type="primary" icon="el-icon-plus" @click="showAddSpuForm"
          >添加SPU</el-button
        >
        <el-table border style="width: 100%" :data="spuList">
          <el-table-column
            align="center"
            type="index"
            prop="prop"
            label="序号"
            width="80"
          >
          </el-table-column>
          <el-table-column prop="spuName" label="SPU名称" width="width">
          </el-table-column>
          <el-table-column prop="description" label="SPU描述" width="width">
          </el-table-column>
          <el-table-column prop="prop" label="SPU操作" width="width">
            <template slot-scope="{ row }">
              <HintButton
                type="success"
                size="mini"
                icon="el-icon-plus"
                title="添加sku"
                @click="showAddSkuForm(row)"
              ></HintButton>
              <HintButton
                type="warning"
                size="mini"
                icon="el-icon-edit"
                title="修改spu"
                @click="showUpdateSpuForm(row)"
              ></HintButton>
              <HintButton
                type="info"
                size="mini"
                icon="el-icon-info"
                title="查看spu下的sku列表"
                @click="showSkuList(row)"
              ></HintButton>
              <HintButton
                type="danger"
                size="mini"
                icon="el-icon-delete"
                title="删除spu"
                @click="deleteSpu(row)"
              ></HintButton>
            </template>
          </el-table-column>
        </el-table>

        <el-pagination
          @size-change="handleSizeChange"
          @current-change="getSpuList"
          style="text-align: center"
          :current-page="page"
          :page-sizes="[3, 5, 7]"
          :pager-count="7"
          :page-size="limit"
          layout="prev, pager,next, jumper,->,sizes ,total"
          :total="total"
        >
        </el-pagination>
      </div>

      <!-- 添加sku界面 -->
      <SkuForm
        ref="sku"
        v-show="isShowSkuForm"
        :visible.sync="isShowSkuForm"
      ></SkuForm>

      <!-- 添加或修改spu -->
      <SpuForm
        ref="spu"
        v-show="isShowSpuForm"
        :visible.sync="isShowSpuForm"
        @backSuccess="backSuccess"
        @cancelBack="cancelBack"
      ></SpuForm>
    </el-card>

    <!-- sku的列表项 -->
    <el-dialog
      :title="`${spu.spuName}的Sku列表`"
      :visible.sync="dialogVisible"
      :before-close="handleClose"
    >
      <el-table border style="width: 100%" :data="skuList">
        <el-table-column prop="skuName" label="名称" width="width">
        </el-table-column>
        <el-table-column prop="price" label="价格" width="width">
        </el-table-column>
        <el-table-column prop="weight" label="重量" width="width">
        </el-table-column>
        <el-table-column prop="prop" label="默认图片" width="width">
          <template slot-scope="{ row }">
            <img
              :src="row.skuDefaultImg"
              alt=""
              style="width: 100px; height: 100px"
            />
          </template>
        </el-table-column>
      </el-table>
    </el-dialog>
  </div>
</template>

<script>
import SkuForm from "@/views/product/spu/components/SkuForm";
import SpuForm from "@/views/product/spu/components/SpuForm";

export default {
  name: "SpuList",
  data() {
    return {
      page: 1,
      limit: 3,
      total: 0,
      category1Id: "",
      category2Id: "",
      category3Id: "",
      dialogVisible: false,
      skuList: [],
      spuList:[],
      spu: [],
      loading: true,
      isShowCategory:true,

      isShowSpuForm: false,
      isShowSkuForm: false,
    };
  },
  components: { SkuForm, SpuForm },
  watch:{
    isShowSpuForm(newVal,oldVal){
      this.isShowCategory = !newVal
    },
    isShowSkuForm(newVal,oldVal){
      this.isShowCategory = !newVal
    }
  },
  methods: {
    changeCategory({ categoryId, level }) {
      if (level === 1) {
        this.category1Id = categoryId;
        this.spuList = [];
        this.category2Id = "";
        this.category3Id = "";
      } else if (level === 2) {
        this.category2Id = categoryId;
        this.spuList = [];
        this.category3Id = "";
      } else {
        this.category3Id = categoryId;
        this.getSpuList();
      }
    },

    handleSizeChange(size) {
      this.limit = size;
      this.getSpuList();
    },
    // 获取spu列表
    async getSpuList(pager = 1) {
      this.pager = pager;
      try {
        const result = await this.$API.spu.getList(
          this.page,
          this.limit,
          this.category3Id
        );
        if (result.code === 200 || result.code === 20000) {
          this.spuList = result.data.records;
          this.total = result.data.total;
        }
        // this.$message.success("获取spu列表成功");
        // console.log(this.spuList)
      } catch (error) {
        this.$message.error("获取spu列表失败" + error.message);
      }
    },

    // 点击添加spu
    showAddSpuForm() {
      this.isShowSpuForm = true;
      this.$refs.spu.getInitAddSpuFormData(this.category3Id);
    },

    // 点击修改spu
    showUpdateSpuForm(row) {
      this.flag = row.id;
      this.isShowSpuForm = true;
      this.$refs.spu.getInitUpdateSpuFormData(row, this.category3Id);
    },

    // 返回成功的回调
    backSuccess() {
      if (this.flag) {
        // 修改回来的
        this.getSpuList(this.page);
      } else {
        // 添加回来的
        this.getSpuList();
      }
    },
    // 取消回来要清空标志位
    cancelBack() {
      this.flag = null;
    },

    // 删除SPU
    async deleteSpu(row) {
      try {
        const result = await this.$API.spu.remove(row.id);
        if (result.code === 20000 || result.code === 200) {
          this.$message.success("删除SPU成功");
          this.getSpuList(this.spuList.length > 1 ? this.page : this.page - 1);
        } else {
          this.$message.error("删除SPU失败");
        }
      } catch (error) {
        this.$message.error("删除SPU请求发送失败");
      }
    },

    // 添加SKU
    showAddSkuForm(row) {
      this.isShowSkuForm = true;
      this.$refs.sku.getInitAddSkuFormData(row);
    },

    // 显示Sku列表
    async showSkuList(row) {
      this.spu = row;
      // console.log(row)
      this.dialogVisible = true;
      this.loading = true;
      try {
        const result = await this.$API.sku.getListBySpuId(row.id);
        if (result.code === 20000 || result.code === 200) {
          this.skuList = result.data;
          this.loading = false;
          // this.$message.success("获取Sku列表成功");
        } else {
          this.$message.error("获取Sku列表失败");
        }
      } catch (error) {
        this.$message.error("请求Sku列表失败");
      }
    },

    handleClose() {
      this.skuList = []
      this.dialogVisible = false;
      
    },
  },
};
</script>

<style>
</style>
